<?php
/*
Description: A PHP class to import Sina blogs into Wordpress-based Lyceum blog system.
Author: Xuefeng LIU, xfliu.math@gmail.com 
		Graduate School of Mathematical Sciences, The University of Tokyo,
		153-8914, Japan	

How to use it:

* Download this file form http://www.xys-reader.org/o/sinablog.tar.gz;
* Extract the file "sinablog.php" from this package and put it into the following path:
  <lyceum folder>wp-admin/import (After 1.0)
  <lyceum folder>lib/wp-admin/import (Before 1.0)

Copyright: This code is free of charge. Anyone can change it and distribute it again with leaving this copyright info above inside.

The author does not take any responsibility for the running results of this code.


Date: 2007/07/28

Last updated:2007/09/21

*/

error_reporting(E_ERROR | E_PARSE);
error_reporting( E_PARSE);

class SinaBlog_Import {

	var $posts = array ();
	var $uid;
	var $file;
	var $rss_data_index;
	var $rss_data_value;
	var $begin_str="\"articleContent\"";
	var $end_str='"articleInfo"';
	
	// parse_leve, GetItem, ReadRss are used to read an xml file and parse it.
	function ParseEachLevel($name,$start,$level){
		
		$obj="";
		$i=$start;
		$j=0;

		while(! ($this->rss_data_value[$i]['type']=="close" and $this->rss_data_value[$i]['tag'] == $name )){

			if($this->rss_data_value[$i]['type']=="complete" and $this->rss_data_value[$i]['tag'] == $name ){
				$obj[$this->rss_data_value[$i]['tag']]=$this->rss_data_value[$i]['value'];
				$i++;	
				next;
			}
			if($this->rss_data_value[$i]['type']=="open"){
				$i++;
				$obj[$this->rss_data_value[$i-1]['tag']]=$this->ParseEachLevel($this->rss_data_value[$i-1]['tag'],&$i,$level+1);
				next;
			}
			$i++;
			$j++;
			if($j>100) break;
		}
		$start=$i+1;
		return $obj;
	}


	function GetItem($keyword,$ind=-1){

		$item_list=$this->rss_data_index[$keyword];
		$num=count($item_list );
		if($ind==-1){
			$j=0;
			while($j<$num){
				$obj[]=$this->ParseEachLevel($keyword,$item_list[$j],0);
				$j++;
			}
		}
		else{
			$item_start=$item_list[$ind];
			$obj=$this->ParseEachLevel($keyword,$item_start,0);
		}
		return $obj;
	}

	function ReadRss($URL,$id=0)
	{
		$this->rss_data_index=NULL;
		$this->rss_data_value=NULL;
		$aurl=fopen($URL,"r");
		stream_set_timeout($aurl, 5);
		if(!$aurl) return NULL;
		while(!feof($aurl)){
			$xml.=fgets($aurl,4096);
		}
		fclose($aurl);

		$parser = xml_parser_create();
		xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING,0);
		xml_parse_into_struct($parser,$xml,$this->rss_data_value,$this->rss_data_index);
		xml_parser_free($parser);
		}

	function get_file_body($URL,$times=1){
	/* This funciton is used to parse an Sina blog article. You see, when the structure of 
	sina blog article changes, the function need to be updated.
	*/
		$end_str=$this->end_str;
		$begin_str=$this->begin_str;

		$aurl=fopen($URL,"r");
		stream_set_timeout($aurl, 5);
		$start=0;
		if(!$aurl) return NULL;
		//echo $begin_str;
		//echo $end_str;
		$str="";
		while(!feof($aurl) ){
			$str.=fgets($aurl,114096);
		}
		if($times>1){ 
				echo "<br>|-| <!-- \n";
				//echo $str;
				echo " \n-->|-|";
		}
/*
		while(!feof($aurl) ){
			$str=fgets($aurl,4096);
			if( preg_match("|$end_str|", $str) and $start==1 ){
				$start=0;
				break;
			}

			if ($start==1){
				$body.=$str;
			}
			if( preg_match("/<div([^<]*)$begin_str([^>]+)>(.*)$/", $str,$matched) ){
				$start=1;
				$body=$matched[3];
			}
			//echo "-";
		}
*/
		fclose($aurl);
		$str=preg_replace("/\n/"," ",$str);

		//preg_match("/$begin_str>(.*)<\/div>/", $str,$matched) ;
		preg_match("/$begin_str>(.*)<\/div>([^<]{1,20})<div class=\"articleInfo\">/", $str,$matched) ;
		$body=$matched[1];
		//echo $URL; print_r($matched[1]); exit(0);
		//echo $str;
		//$body=preg_replace("/\n\n/","\n",$body);
		return $body;
	}

	function header() {
		echo '<div class="wrap">';
		echo '<h2>'.__('Import Sina Blogs').'</h2>';
	}

	function footer() {
		echo '</div>';
	}

	function unhtmlentities($string) { // From php.net for < 4.3 compat
		return $string;
	}

	function greet() {
		global $wpdb,$blog,$user, $current_user;
		$my_user_ID=$_GET["user_id"];



		echo "<!-- Hi, $my_user_ID --->";

		$sina_url=get_settings("sina_blog");
		if(!$sina_url) $sina_url='http://blog.sina.com.cn/???';
		echo '<p>'.__('Howdy! This importer allows you to import Sina blogs').'</p>';
		echo '<form  method="post" action="admin.php?import=sinablog&step=1">';

		if($current_user->user_admin || 1){
			echo __("Set the author of imported article to be:");

			echo '<select name="user_ID"> '; 

  			$all_logins = $wpdb->get_results("SELECT DISTINCT ID, user_login FROM $wpdb->users, $wpdb->usermeta WHERE blog = '$blog' and ID = user_id ORDER BY user_login");
        		foreach ($all_logins as $login) {
                        	echo "<option value=\"{$login->ID}\">{$login->user_login}</option>";
        		}
			echo '</select>'; 



/*
			echo ' <input name="user_ID" value="'; 
			echo $my_user_ID;
			echo '" type="text" size=3>';	
			echo " (Please input user ID Number here. Default user is the admin. )";
*/

			echo "<br><br>";
		}
		else {
			echo '<input name="user_ID" value="';
			echo $my_user_ID;
			echo '" type="hidden">';
		}
		
		echo _('Please input the homepage url of your sina blog');

		echo '
		<br><br>
	<div id="buttons">
	<label for="upload">';
		echo __("Homepage URL:"), '</label> <input id="upload" name="import_url" size=40 type="text" value="'.$sina_url.'">';
		echo '<input name="action" value="save" type="hidden">';
	
	echo ' <input value="'. __('Import It') . '" type="submit">
	</div>
	</form>
<br>
<br>
		<hr size=1 width=80% align=left>';
		echo '

Created by Liu Xuefeng for XYS-READER BLOG, 2007/08<br>
		If you have any problem during the importing, please contact with xfliu.math@gmail.com; 
		';
	}

	function get_posts($URL) {
		global $wpdb;
		global $user_ID;
		set_time_limit(50);


		$my_user_ID=$_POST["user_ID"];


		if(!$my_user_ID){
			$my_user_ID=$user_ID;
		}
		
		set_magic_quotes_runtime(0);
		
		$uid=$_POST['import_uid'];

		echo "<!-- Hi,",$my_user_ID,"  --->";
		

		foreach($_POST['import_cat'] as $cat){
			
			
			$page_over=0;
			$page=0;
			while(!$page_over){
				$page++;
				$URL="http://blog.sina.com.cn/sns/service.php?m=aList&uid=${uid}&sort_id=${cat}&page=${page}";
				$this->ReadRss($URL);
				$Titles=$this->GetItem("title");

				$Dates=$this->GetItem("sns:pubDate");
				$Links=$this->GetItem("link");
				
				$post_catalog = "未分类";
				if($cat>0){
					$post_catalog = preg_replace("/^([^\-]+)-/","",$Titles[0]["title"]);
					$post_catalog = trim($post_catalog);
				}
				
				echo "<br><b>";	
				echo __("Reading article in page").  $page." of ", $post_catalog  ," <br> \n ";
				echo "URL: ", $URL,"<br></b>\n";

		
				$i=0;
				$index=0;
				unset ($posts);
				$err_num=0;
				foreach( $Links as $link){
					//if($err_num>3) {$page_over=1; break;}
					
					if($link["link"] and $link["link"] !="sns:index"  ){
						$mytitle=$Titles[$i+1];
						$post["title"]=$mytitle["title"];
						$post_title = $wpdb->escape(trim($post["title"]));
						$post["date"]=$Dates[$i]["sns:pubDate"];
						if ($post["date"]) {
							$post_date = substr($post["date"],0,10);
							$post_date.=" ";
							$post_date .= substr($post["date"],13,8);
						} else {
							//$post_date = gmdate('Y-m-d H:i:s', $post_date);
							$post_date = "";
						}

						$i_str=$i+1;
						if($i_str<10) $i_str="0".$i_str;
						echo "Reading $i_str th article: Date:",$post_date, "&nbsp; ",$post["title"], " ";
						
						if(!$_POST["my_update_post"] && $post_id = post_exists($mytitle["title"], "" ,substr($post_date,0,20))){
							echo " (";
							_e('Post already imported');
							echo ") ";
							if ($post_catalog and $post_id and $post_catalog!="未分类")
								wp_create_categories(array($post_catalog), $post_id);
						}
						else{
							$url="http://blog.sina.com.cn".$link["link"];
							$content= $this->get_file_body($url);
							if(!$content){ 
								//$content= $this->get_file_body($url,2);
							}

							if(!$content){ 

								$err_num++; 
								echo "<br><b>$err_num: Error!</b> $url <br>";
								$i++; $index++; continue;
							}
/*
							$post["content"]=$content;
							$post["category"]=array($post_catalog);
							$post["post_category"]=array($post_catalog);
							
							$categories = NULL;
							$categories[0] = $wpdb->escape($this->unhtmlentities($post_catalog));
*/
							/* ----------------------------------------------------
							// preparation for inserting into database.
							----------------------------------------------------  */							

							if ($guid)
								$guid = $wpdb->escape(trim($guid[1]));
							else
								$guid = '';

							$post_content = $content;

							// Clean up content
							$post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
							$post_content = str_replace('<br>', '<br />', $post_content);
							$post_content = str_replace('<hr>', '<hr />', $post_content);
							$post_content = str_replace('<div />', '', $post_content);
							$post_content = str_replace('<p>&nbsp;</p>', '', $post_content);

							$post_author = $my_user_ID;
							$post_status = 'publish';
							
							$this->posts = compact('post_author', 'post_date','post_date_gmt', 'post_content', 'post_title', 'post_status', 'guid', 'categories');
							$this->import_posts();

							//exit();
							/* ----------------------------------------------------
							// preparation for inserting into database. OVER
							----------------------------------------------------  */							
							
						}
						$i++;
						$index++;
					}
					echo "<br> \n ";
				}
				echo __("Articles in this page finished."), "<br> \n ";

				if(!$index || $_POST["only_first_page"] ){ $page_over=1; break; }

				//sleep(2);

			}

		}

	}

	function import_posts() {

			global $is_import;
			$is_import=1;

			$post=$this->posts;

			extract($post);
			
			if($post_title and $post_content){

				if ($post_id = post_exists($post_title, $post_content)) {

					if($_POST['my_update_post']){
						$post["ID"]=$post_id;
						if (!$post_id=wp_update_post($post)) {
							_e("Couldn't update this post");
							return;
						}
						_e(" updating ");
					}
					else{
						_e('Post already imported');
						return;
					}
				} else {
					if (!$post_id=wp_insert_post($post)) {
						_e("Couldn't get post ID");
						return;
					}

					if (0 != count($categories) )
						wp_create_categories($categories, $post_id);
				}
			}
		echo " [ ";
		_e('Done !');
		echo " ]";

		$is_import=0;

	}
	function import() {
		$file = wp_import_handle_upload();
		echo $_POST['import_url'];
		$this->get_posts($_POST['import_url']);
		wp_import_cleanup($file['id']);
		
		echo '<h3>';
		printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
		echo '</h3>';
	}
	function cat_list(){

                global $user_ID,$current_user;
                $my_user_ID=$_POST["user_ID"];
                if(!$my_user_ID){
                     $my_user_ID=$user_ID;
                }
		if($current_user->user_admin){
			echo "User id: $my_user_ID.<br>\n";
		}

		
		$url=$_POST['import_url'];
		update_option("sina_blog",$url);
		echo __("Your blog URL:"). $url."<br>\n";
		
		$fh=fopen($url,"r");
		stream_set_timeout($fh, 5);
		if(!$fh) return NULL;
		while(!feof($fh)){
			$txt=fgets($fh,4096);
			if( preg_match('/<title>(.+)<\/title>/',$txt,$matched)){
				$blog_title=$matched[1];
			}
			if( preg_match('/blog(.+)src=http:\/\/([^\.]+)\./',$txt,$matched)){
				$blog_frame=$matched[2];
			}
			if( preg_match('/UID([^\d]*)([\d]{5,15})/i',$txt,$matched)){
				$uid=$matched[2];
				break;
			}
		}
		fclose($fh);
	
		if(!$uid){

                $fh=fopen("http://".$blog_frame.".".substr($url,7,30),"r");
               // $fh=fopen("http://v35.".substr($url,7,30),"r");
                if(!$fh) return NULL;
                while(!feof($fh)){
                        $txt=fgets($fh,4096);
			stream_set_timeout($fh,5);
                        if( preg_match('/<title>(.+)<\/title>/',$txt,$matched)){
                                $blog_title=$matched[1];
                        }
                        if( preg_match('/blog(.+)src=http:\/\/([^\.]+)\./',$txt,$matched)){
                                $blog_frame=$matched[2];
                        }
                        if( preg_match('/UID([^\d]*)([\d]{5,15})/i',$txt,$matched)){
                                $uid=$matched[2];
                                break;
                        }
                }
                fclose($fh);

		}


		$this->uid=$uid;
		
		echo __("Your blog title:"). $blog_title."<br>\n";
		echo __("Your blog UID:") . $uid. "<br>\n";
		
		$sortid=1;		$no_re=0;

		if(!$sina_url) $sina_url='http://blog.sina.com.cn/???';

		echo "<!-- Hi, $my_user_ID --->";

		echo '<form  method="post" action="admin.php?import=sinablog&step=2">';
		
		echo "<ul>";
		while(1){
		
			$URL="http://blog.sina.com.cn/sns/service.php?m=aList&uid=${uid}&sort_id=${sortid}";
			$this->ReadRss($URL);
			$Titles=$this->GetItem("title");
			if($Titles[0][title]){
				echo "<li>".$Titles[0][title]."<input type='checkbox' name=\"import_cat[]\" value=\"$sortid\">";
			}else{
				$no_re++;}
			$sortid++;	
			if($no_re>2) break;
		}
		$sortid=0;
		$URL="http://blog.sina.com.cn/sns/service.php?m=aList&uid=${uid}&sort_id=${sortid}";
		$this->ReadRss($URL);
		$Titles=$this->GetItem("title");
		if($Titles[0][title] ){
			echo "<li>".$Titles[0][title]."<input type='checkbox' name=\"import_cat[]\" value=\"$sortid\">";	}
		
		echo "</ul><br>";
		echo "Only import first page (the latest one) of each category? <input type='checkbox' name='only_first_page' >\n<br>";
		echo "Update the post that has already been imported? <input type='checkbox' name='my_update_post' >\n";
		echo "<input type='hidden' name='import_uid' value='$uid'>\n";
		echo '<input name="user_ID" value="'.$my_user_ID.'" type="hidden">';
		echo '<div id="buttons"><input value="'. __('Import It') . '" type="submit">
			  <input value="Cancel" onclick="cancelUpload()" type="button"></div></form>';
	
	}

	function dispatch() {
		if (empty ($_GET['step']))
			$step = 0;
		else
			$step = (int) $_GET['step'];

		$this->header();
		
		switch ($step) {
			case 0 :
				$this->greet();
				break;
			case 1 :
				$this->cat_list();
				break;
			case 2 :
				$this->import();
				break;
		}
		
		$this->footer();
	}

	function SinaBlog_Import() {
		// Nothing.	
	}
}

$sina_import = new SinaBlog_Import();

register_importer('sinablog', 'SINA', __('Import posts from a Sina Blog'), array ($sina_import, 'dispatch'));
?>
